<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="NAME">
        <el-input
          v-model="queryParams.NAME"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专业" prop="PROFESSION">
        <el-input
          v-model="queryParams.PROFESSION"
          placeholder="请输入专业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地区" prop="REGION">
        <el-cascader
                    v-model="form.REGION"
                    :options="regionData"
                    :size="medium"
                  />
        <!-- <el-input
          v-model="queryParams.REGION"
          placeholder="请输入地区"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>
      <el-form-item label="身份证" prop="IDCARD">
        <el-input
          v-model="queryParams.IDCARD"
          placeholder="请输入身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="PHONE">
        <el-input
          v-model="queryParams.PHONE"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="UNTI">
        <el-input
          v-model="queryParams.UNTI"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
          <el-date-picker
            v-model="queryParams.createTime"
            style="width: 240px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </el-form-item>
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="年龄" prop="AGE">
          <el-input
            v-model="queryParams.AGE"
            placeholder="请输入年龄"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
       
        <el-form-item label="邮箱" prop="EMAIL">
          <el-input
            v-model="queryParams.EMAIL"
            placeholder="请输入邮箱"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        
        
        <el-form-item label="等级" prop="GRADE">
          <el-input
            v-model="queryParams.GRADE"
            placeholder="请输入等级"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        
        <el-form-item label="毕业院校" prop="UNIVERSITY">
          <el-input
            v-model="queryParams.UNIVERSITY"
            placeholder="请输入毕业院校"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="是否退休；退休：1，未退休：2" prop="RETIRE">
          <el-input
            v-model="queryParams.RETIRE"
            placeholder="请输入是否退休；退休：1，未退休：2"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="更新人" prop="updateBy">
          <el-input
            v-model="queryParams.updateBy"
            placeholder="请输入更新人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="更新时间" prop="updateTime">
          <el-date-picker clearable
            v-model="queryParams.updateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
         -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:library:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:library:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:library:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:library:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="libraryList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column label="编号" align="left" type="index"/>
      <el-table-column label="姓名" align="left" prop="exName" >
        <template slot-scope="scope">
          <a @click.prevent="handleLinkClick(scope.row)" style="color: #409eff">
            {{ scope.row.exName }}
          </a>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="left" prop="exIdcard"  width="200"/>
      <el-table-column label="专业" align="left" prop="exProfe" />
      <el-table-column label="手机号" align="left" prop="exPhone" />
      <el-table-column label="性别" align="left" prop="exSex" />
      <el-table-column label="地区" align="left" prop="exRegion" />
      <el-table-column label="单位" align="left" prop="UNTI" />

      <el-table-column label="年龄" align="left" prop="exAge" />
      <el-table-column label="创建人" align="left" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="left"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:library:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:library:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改专家库对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1100px"
      append-to-body
    >
      <div style="background-color: #fff; padding: 20px">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="姓名" prop="NAME">
                <el-input v-model="form.NAME" placeholder="请输入姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="专业" prop="PROFESSION">
                <el-input v-model="form.PROFESSION" placeholder="请输入专业" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="毕业院校" prop="UNIVERSITY">
                <el-input
                  v-model="form.UNIVERSITY"
                  placeholder="请输入毕业院校"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="身份证" prop="IDCARD">
                <el-input
                  v-model="form.IDCARD"
                  placeholder="请输入身份证"
                  onkeyup="this.value=this.value.replace(/[^\X0-9]/g, '')"
                  maxlength="18"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="年龄" prop="AGE">
                <el-input v-model="form.AGE" placeholder="请输入年龄" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="性别" prop="SEX">
                <el-select
                  v-model="form.SEX"
                  placeholder="请选择性别"
                  style="width: 100%"
                  :size="medium"
                >
                  <el-option
                    v-for="dict in dict.type.sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="手机号" prop="PHONE">
                <el-input
                  v-model="form.PHONE"
                  placeholder="请输入手机号"
                  onkeyup="this.value=this.value.replace(/[^\X0-9]/g, '')"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="邮箱" prop="EMAIL">
                <el-input v-model="form.EMAIL" placeholder="请输入邮箱" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="区域" prop="REGION">
                <el-input v-model="form.REGION" placeholder="请输入区域" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="等级" prop="GRADE">
                <el-input v-model="form.GRADE" placeholder="请输入等级" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="工作单位" prop="UNTI">
                <el-input v-model="form.UNTI" placeholder="请输入工作单位" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="岗位" prop="REGION">
                <el-input v-model="form.REGION" placeholder="请输入岗位" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listLibrary,
  getLibrary,
  delLibrary,
  addLibrary,
  updateLibrary,
} from "@/api/system/library";
import { regionData } from 'element-china-area-data'
export default {
  name: "Library",
  dicts: ["sys_user_sex"],
  data() {
    // 身份证号校验
    var checkIdNum = (rule, value, callback) => {
      const reg =
        /(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)/;
      console.log("---value---", value);
      var org_gender = value.substring(16, 17);
      var sex = org_gender % 2 == 1 ? "男" : "女";
      let myDate = new Date();
      let month = myDate.getMonth() + 1;
      let day = myDate.getDate();
      let age = 0;
      age = myDate.getFullYear() - value.substring(6, 10) - 1;
      if (!value) {
        return callback(new Error("证件号码不能为空"));
      } else if (!reg.test(value)) {
        return callback(new Error("证件号码不正确"));
      } else {
        callback();
      }

      this.form.SEX = sex;
      this.form.AGE = age;
    };
    var checkPhone = (rule, value, callback) => {
      const pattern = /^1[3456789]\d{9}$/;
      if (!value) {
        return callback(new Error("手机号码不能为空"));
      } else if (!pattern.test(value)) {
        return callback(new Error("手机号码不正确"));
      } else {
        callback();
      }
    };
    const checkEmail = (rule, value, callback) => {
      const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
      if (!value) {
        return callback(new Error("邮箱不能为空"));
      } else if (!mailReg.test(value)) {
        callback(new Error("请输入正确的邮箱格式"));
      } else {
        callback();
      }
    };
    return {
      regionData,
      selectedOptions: [],
      // 输入框的size
      medium: "medium",
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专家库表格数据
      libraryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        NAME: undefined,
        IDCARD: undefined,
        AGE: undefined,
        PHONE: undefined,
        EMAIL: undefined,
        PROFESSION: undefined,
        SEX: undefined,
        REGION: undefined,
        GRADE: undefined,
        UNTI: undefined,
        UNIVERSITY: undefined,
        RETIRE: undefined,
        updateBy: undefined,
        updateTime: undefined,
        createBy: undefined,
        createTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ID: [{ required: true, message: "不能为空", trigger: "blur" }],
        NAME: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        IDCARD: [
          { required: true, message: "身份证不能为空", trigger: "blur" },
          {
            validator: checkIdNum,
            trigger: "blur",
          },
        ],
        AGE: [{ required: true, message: "年龄不能为空", trigger: "blur" }],
        PHONE: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            validator: checkPhone,
            trigger: "blur",
          },
        ],
        EMAIL: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
          {
            validator: checkEmail,
            trigger: "blur",
          },
        ],
        PROFESSION: [
          { required: true, message: "专业不能为空", trigger: "blur" },
        ],
        SEX: [
          {
            required: true,
            message: "性别；男：1，女：2不能为空",
            trigger: "change",
          },
        ],
        REGION: [{ required: true, message: "区域不能为空", trigger: "blur" }],
        GRADE: [{ required: true, message: "等级不能为空", trigger: "blur" }],
        UNTI: [{ required: true, message: "单位不能为空", trigger: "blur" }],
        UNIVERSITY: [
          { required: true, message: "毕业院校不能为空", trigger: "blur" },
        ],
        RETIRE: [
          {
            required: true,
            message: "是否退休；退休：1，未退休：2不能为空",
            trigger: "blur",
          },
        ],
        updateBy: [
          { required: true, message: "更新人不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询专家库列表 */
    getList() {
      this.loading = true;
      listLibrary(this.queryParams).then((response) => {
        this.libraryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleLinkClick(row){
      getLibrary(row.exId).then((response) => {
        this.$router.push({
        path: "/review/exportBaseAdd",
        query: {title:"专家信息详情" ,row: response,saleslead: 1},
      });
      })
    },
    
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ID: undefined,
        NAME: undefined,
        IDCARD: undefined,
        AGE: undefined,
        PHONE: undefined,
        EMAIL: undefined,
        PROFESSION: undefined,
        SEX: undefined,
        REGION: undefined,
        GRADE: undefined,
        UNTI: undefined,
        UNIVERSITY: undefined,
        RETIRE: undefined,
        updateBy: undefined,
        updateTime: undefined,
        createBy: undefined,
        createTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.exId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: "/review/exportBaseAdd",
        query: {title:"专家信息录入" },
      });
      // this.reset();
      // this.open = true;
      // this.title = "添加专家库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(this.ids)
      this.loading = true;
      this.reset();
      const ID = row.ID || this.ids;
      // getLibrary(ID).then((response) => {
        // this.loading = false;
        // this.form = response.data;
        // this.open = true;
        // this.title = "修改专家库";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.ID != null) {
            updateLibrary(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addLibrary(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const IDs = row.ID || this.ids;
      this.$modal
        .confirm('是否确认删除专家库编号为"' + IDs + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delLibrary(IDs);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/library/export",
        {
          ...this.queryParams,
        },
        `library_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
  